#!/bin/sh -x
#
# save exec commonds using
# ./build_osc.sh config debug 2>&1 |grep '^+' |grep configure
# ./build_osc.sh build debug 2>&1 > info.txt
#
# x264/x265 need nasm > 2.13
#

# cd ~/workspace/git
# cd /e/workspace/git
# cd /w/workspace/git

BUILD_SCRIPT_DIR=$PWD

if [ 1 -gt $# ]; then
    echo $#
    echo "Usage: $0 [config|build|rebuild|clean] [debug|release|all] [linux|android] [x32|x64][armeabi-v7a]"
    exit 1
fi

# BUILD_PROJS="openssl curl jsoncpp SDL osip exosip jthread jrtplib libevent x264 ffmpeg"
# BUILD_PROJS="openssl"
# BUILD_PROJS="curl"
# BUILD_PROJS="openssl curl"
# BUILD_PROJS="jsoncpp"
# BUILD_PROJS="SDL"
# BUILD_PROJS="osip"
# BUILD_PROJS="exosip"
# BUILD_PROJS="osip exosip"
# BUILD_PROJS="jthread jrtplib"
# BUILD_PROJS="libevent"
# BUILD_PROJS="x264"
# BUILD_PROJS="x265"              # need nasm >= 2.13
# BUILD_PROJS="ffmpeg"
# BUILD_PROJS="openssl x264 ffmpeg jthread jrtplib SDL"  # mingw , openssl only 1.0.1x version
# BUILD_PROJS="x264 ffmpeg"  # mingw-msvc, note: x265 is implemented in C++ and can not be statically linked with ffpmeg, only using the dynamic library
# BUILD_PROJS="openssl curl jsoncpp x264 ffmpeg"         # android
# BUILD_PROJS="log4x"
# BUILD_PROJS="log4x j2x"
# BUILD_PROJS="stk"
# BUILD_PROJS="easySQLite"
# BUILD_PROJS="jpeg"
# BUILD_PROJS="libjpeg-turbo"      # need nasm >= 2.13
# BUILD_PROJS="net-snmp-5.8"       # need sudo mv /usr/include/openssl /usr/include/openssl_
# BUILD_PROJS="googletest"

ACTION=$1
TYPE=$2
OS=$3
ARCH=$4

BUILD_OS=
BUILD_ARCH=
BUILD_TYPE=

BUILD_GIT_ROOT=
BUILD_CFG_FLAGS=

BUILD_DEBUG_POSTFIX=

BUILD_TOOLCHAIN_DIR=
BUILD_TOOLCHAIN_PREFIX=

BUILD_CMAKE_FLAGS=

BUILD_MAKE_ARGS=

BUILD_CFLAGS="-fPIC"
BUILD_EXTRA_CFLAGS=

function parse()
{
    case $OS in
        linux)
            BUILD_OS=linux
            ;;

        android)
            BUILD_OS=android

            BUILD_TOOLCHAIN_DIR=/usr/local/android-ndk-standalone/r10e/arm-linux-androideabi-4.9
            # BUILD_TOOLCHAIN_DIR=/usr/local/android-ndk-standalone/r15c/arm-linux-androideabi-4.9.x
            BUILD_TOOLCHAIN_PREFIX=${BUILD_TOOLCHAIN_DIR}/bin/arm-linux-androideabi-
            BUILD_TOOLCHAIN="CC=${BUILD_TOOLCHAIN_PREFIX}gcc CXX=${BUILD_TOOLCHAIN_PREFIX}g++ AR=${BUILD_TOOLCHAIN_PREFIX}ar"
            BUILD_CROSS_SYSROOT=${BUILD_TOOLCHAIN_DIR}/sysroot

            BUILD_CMAKE_FLAGS="$BUILD_CMAKE_FLAGS -DCMAKE_TOOLCHAIN_FILE=$BUILD_SCRIPT_DIR/android.toolchain.cmake -DJSONCPP_WITH_TESTS:BOOL=OFF"
            BUILD_MAKE_ARGS="$BUILD_MAKE_ARGS ANDROID_DEV=${BUILD_CROSS_SYSROOT} CROSS_SYSROOT=${BUILD_CROSS_SYSROOT}"

            BUILD_CFG_FLAGS="$BUILD_CFG_FLAGS --host=arm-linux"
            BUILD_CFLAGS="$BUILD_CFLAGS -march=armv7-a -mfloat-abi=softfp -mfpu=neon -mthumb"
            ;;

        mingw)
            BUILD_OS=mingw
            # BUILD_GIT_ROOT=/e/workspace/git
            # BUILD_GIT_ROOT=/w/workspace/git
            ;;

        mingw-msvc)
            BUILD_OS=mingw-msvc
            # BUILD_GIT_ROOT=/e/workspace/git
            # BUILD_GIT_ROOT=/w/workspace/git
            BUILD_CFG_FLAGS="$BUILD_CFG_FLAGS --toolchain=msvc --pkg-config=pkg-config"
            ;;
        *)
            BUILD_OS=linux
            ;;
    esac

    case $ARCH in
        x64)
            BUILD_ARCH=x64
            BUILD_CFG_FLAGS="$BUILD_CFG_FLAGS --arch=x86_64"
            ;;
        x32)
            BUILD_ARCH=x32
            ;;
        *)
            if [ $BUILD_OS = android ]; then
                BUILD_ARCH=armeabi-v7a
            else
                BUILD_ARCH=x64
            fi
            ;;
    esac

    case $TYPE in
        debug)
            BUILD_TYPE=debug
            BUILD_DEBUG_POSTFIX=d
            BUILD_CFLAGS="$BUILD_CFLAGS -g"
            BUILD_CFG_FLAGS="$BUILD_CFG_FLAGS --enable-debug"
            BUILD_EXTRA_CFLAGS="-MDd"
            ;;
        release)
            BUILD_TYPE=release
            BUILD_CFLAGS="$BUILD_CFLAGS -O3"
            BUILD_CFG_FLAGS="$BUILD_CFG_FLAGS --disable-debug"
            BUILD_EXTRA_CFLAGS="-MD"
            ;;
        *)
            BUILD_TYPE=debug
            BUILD_DEBUG_POSTFIX=d
            BUILD_CFLAGS="$BUILD_CFLAGS -g"
            BUILD_CFG_FLAGS="$BUILD_CFG_FLAGS --enable-debug"
            BUILD_EXTRA_CFLAGS="-MDd"
            ;;
    esac

    cd $BUILD_GIT_ROOT

    echo "BUILD_OS: $BUILD_OS"
    echo "BUILD_ARCH: $BUILD_ARCH"
    echo "BUILD_TYPE: $BUILD_TYPE"

    BUILD_INSTALL=$PWD/build-osc-install
    BUILD_INSTALL_INC=$BUILD_INSTALL/include
    BUILD_INSTALL_LIB=$BUILD_INSTALL/lib/$BUILD_OS/$BUILD_ARCH

    mkdir -p $BUILD_INSTALL_INC $BUILD_INSTALL_LIB

    echo "BUILD_INSTALL: $BUILD_INSTALL"
    echo "BUILD_INSTALL_INC: $BUILD_INSTALL_INC"
    echo "BUILD_INSTALL_LIB: $BUILD_INSTALL_LIB"

    BUILD_SUB_DIR=build_$BUILD_OS/$BUILD_ARCH
    BUILD_SUB_DIR_INSTALL=$BUILD_SUB_DIR/install
    BUILD_SUB_DIR_INC=$BUILD_SUB_DIR_INSTALL/include
    BUILD_SUB_DIR_LIB=$BUILD_SUB_DIR_INSTALL/lib
}

function do_automake()
{
    echo "#################### config $1 using do_automake: configure ..."
    case $1 in
        openssl)
            if [ $BUILD_OS = android ]; then
                ./Configure android --prefix=$PWD/$BUILD_SUB_DIR_INSTALL $BUILD_CFLAGS --cross-compile-prefix="$BUILD_TOOLCHAIN_PREFIX"
            else
                ./config --prefix=$PWD/$BUILD_SUB_DIR_INSTALL $BUILD_CFLAGS
            fi

            # curl config will be dependent on openssl
            make $BUILD_MAKE_ARGS
            make install
            ;;

        curl)
            ./buildconf
            ./configure --prefix=$PWD/$BUILD_SUB_DIR_INSTALL  --disable-shared --with-ssl=$BUILD_GIT_ROOT/openssl/$BUILD_SUB_DIR_INSTALL $BUILD_CFG_FLAGS $BUILD_TOOLCHAIN
            ;;

        osip)
            rm -f configure
            ./autogen.sh
            CFLAGS=$BUILD_CFLAGS ./configure --prefix=$PWD/$BUILD_SUB_DIR_INSTALL --disable-shared

            make
            make install
            ;;

        exosip)
            export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$BUILD_GIT_ROOT/osip/$BUILD_SUB_DIR_LIB/pkgconfig/
            # export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$BUILD_GIT_ROOT/openssl/$BUILD_SUB_DIR_LIB/pkgconfig/
            rm -f configure
            ./autogen.sh
            # ./configure --prefix=$PWD/$BUILD_SUB_DIR_INSTALL --disable-shared PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$BUILD_GIT_ROOT/osip/$BUILD_SUB_DIR_LIB/pkgconfig/:$BUILD_GIT_ROOT/openssl/$BUILD_SUB_DIR_LIB/pkgconfig/" PKG_CONFIG_LIBDIR="$BUILD_GIT_ROOT/openssl/$BUILD_SUB_DIR_LIB/" SSL_CFLAGS="-I$BUILD_GIT_ROOT/openssl/$BUILD_SUB_DIR_INC" SSL_LIBS="-L$BUILD_GIT_ROOT/openssl/$BUILD_SUB_DIR_LIB -lssl -lcrypto"
            CFLAGS=$BUILD_CFLAGS ./configure --prefix=$PWD/$BUILD_SUB_DIR_INSTALL --disable-shared --disable-openssl
            ;;

        x264)
            if [ $BUILD_OS = mingw-msvc ]; then
                CC=cl ./configure --prefix=$PWD/$BUILD_SUB_DIR_INSTALL --enable-static --enable-pic $BUILD_CFG_FLAGS --extra-cflags="$BUILD_EXTRA_CFLAGS"
            else
                ./configure --prefix=$PWD/$BUILD_SUB_DIR_INSTALL \
                    --enable-static \
                    --enable-pic \
                    --enable-strip \
                    --cross-prefix=$BUILD_TOOLCHAIN_PREFIX \
                    --sysroot=$BUILD_CROSS_SYSROOT \
                    $BUILD_CFG_FLAGS \
                    --extra-cflags="$BUILD_CFLAGS"
            fi
            make
            make install
            ;;

        ffmpeg)
            export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$BUILD_GIT_ROOT/x264/$BUILD_SUB_DIR_LIB/pkgconfig/
            export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$BUILD_GIT_ROOT/x265/$BUILD_SUB_DIR_LIB/pkgconfig/

            BUILD_DEBUG=
            if [ $BUILD_TYPE = release ]; then
                BUILD_DBUG=--disable-debug
            fi

            if [ $BUILD_OS = android ]; then
                ./configure --prefix=$PWD/$BUILD_SUB_DIR_INSTALL \
                    $BUILD_DBUG \
                    --enable-static \
                    --enable-gpl \
                    --enable-pic \
                    --disable-lzma \
                    --disable-linux-perf \
                    --target-os=linux \
                    --arch=arm \
                    --sysroot=$BUILD_CROSS_SYSROOT \
                    --pkg-config=pkg-config \
                    --cross-prefix=$BUILD_TOOLCHAIN_PREFIX \
                    --extra-cflags="$BUILD_CFLAGS"
                    #--enable-libx264 \
                    #--enable-libx265 \
            else
                ./configure --prefix=$PWD/$BUILD_SUB_DIR_INSTALL \
                    --enable-static \
                    --enable-gpl \
                    --enable-pic \
                    --disable-lzma \
                    $BUILD_CFG_FLAGS \
                    --extra-cflags="$BUILD_EXTRA_CFLAGS"
                    #--enable-libx264 \
                    #--enable-libx265 \
            fi
            ;;

        jpeg)
            CFLAGS=$BUILD_CFLAGS ./configure --prefix=$PWD/$BUILD_SUB_DIR_INSTALL
            make
            make install
            ;;

        net-snmp-5.8)
            # CFLAGS=$BUILD_CFLAGS ./configure --prefix=$PWD/$BUILD_SUB_DIR_INSTALL --with-openssl=$BUILD_GIT_ROOT/openssl/$BUILD_SUB_DIR_INSTALL --with-ldflags="-ldl"
            CFLAGS=$BUILD_CFLAGS ./configure --prefix=$PWD/$BUILD_SUB_DIR_INSTALL
            make
            make install
            ;;
    esac
}

function do_cmake()
{
    echo "#################### config $1 using cmake: cmake ..."

    case $1 in
        curl)
            cmake \
                -DCMAKE_INSTALL_PREFIX=$PWD/install \
                -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
                -DCMAKE_DEBUG_POSTFIX="$BUILD_DEBUG_POSTFIX" \
                -DCMAKE_C_FLAGS="-fPIC" \
                -DCMAKE_LD_FLAGS="-pthread" \
                -DCMAKE_LDAP_LIB=OFF \
                -DCMAKE_LBER_LIB=OFF \
                -DCURL_STATICLIB=ON \
                -DOPENSSL_ROOT_DIR=$BUILD_GIT_ROOT/openssl/$BUILD_SUB_DIR_INSTALL \
                $BUILD_CMAKE_FLAGS \
                -G "Unix Makefiles" $BUILD_GIT_ROOT/$PROJ/
            ;;

        jsoncpp)
            cmake \
                -DCMAKE_INSTALL_PREFIX=$PWD/install \
                -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
                -DCMAKE_DEBUG_POSTFIX="$BUILD_DEBUG_POSTFIX" \
                -DCMAKE_CXX_FLAGS="-fPIC" \
                -DBUILD_STATIC_LIBS=ON \
                $BUILD_CMAKE_FLAGS \
                -G "Unix Makefiles" $BUILD_GIT_ROOT/$PROJ/
            ;;

        SDL)
            cmake \
                -DCMAKE_INSTALL_PREFIX=$PWD/install \
                -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
                -DCMAKE_DEBUG_POSTFIX="$BUILD_DEBUG_POSTFIX" \
                -DCMAKE_C_FLAGS="-fPIC" \
                -DSDL_STATIC=ON \
                -DSDL_SHARED=OFF \
                $BUILD_CMAKE_FLAGS \
                -G "Unix Makefiles" $BUILD_GIT_ROOT/$PROJ/
            ;;

        jthread)
            cmake \
                -DCMAKE_INSTALL_PREFIX=$PWD/install \
                -DCMAKE_BUILD_TYPE=$BUILD_TYPE\
                -DCMAKE_CXX_FLAGS="-fPIC" \
                $BUILD_CMAKE_FLAGS \
                -G "Unix Makefiles" $BUILD_GIT_ROOT/$PROJ/

            # jrtplib config will be dependent on jthread
            make $BUILD_MAKE_ARGS
            make install
            ;;

        jrtplib)
            cmake \
                -DCMAKE_INSTALL_PREFIX=$PWD/install \
                -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
                -DCMAKE_DEBUG_POSTFIX="$BUILD_DEBUG_POSTFIX" \
                -DCMAKE_CXX_FLAGS="-fPIC" \
                -DJThread_DIR:PATH="$BUILD_GIT_ROOT/jthread/$BUILD_SUB_DIR/cmake" \
                $BUILD_CMAKE_FLAGS \
                -G "Unix Makefiles" $BUILD_GIT_ROOT/$PROJ/
            ;;

        libevent)
            cmake \
                -DCMAKE_INSTALL_PREFIX=$PWD/install \
                -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
                -DCMAKE_DEBUG_POSTFIX="$BUILD_DEBUG_POSTFIX" \
                -DCMAKE_C_FLAGS="-fPIC -ldl" \
                -DBUILD_STATIC_LIBS=ON \
                -DBUILD_SHARED_LIBS=OFF \
                -DOPENSSL_ROOT_DIR=$BUILD_GIT_ROOT/openssl/$BUILD_SUB_DIR_INSTALL \
                -G "Unix Makefiles" $BUILD_GIT_ROOT/$PROJ/
            ;;

        log4x)
            cmake \
                -DCMAKE_INSTALL_PREFIX=$PWD/install \
                -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
                -DCMAKE_DEBUG_POSTFIX="$BUILD_DEBUG_POSTFIX" \
                -DCMAKE_CXX_FLAGS="-fPIC" \
                -DBUILD_STATIC_LIBS=OFF \
                -DBUILD_SHARED_LIBS=ON \
                -G "Unix Makefiles" $BUILD_GIT_ROOT/$PROJ/
            ;;

        j2x)
            cmake \
                -DCMAKE_INSTALL_PREFIX=$PWD/install \
                -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
                -DCMAKE_DEBUG_POSTFIX="$BUILD_DEBUG_POSTFIX" \
                -DCMAKE_CXX_FLAGS="-fPIC" \
                -DBUILD_STATIC_LIBS=ON \
                -DBUILD_SHARED_LIBS=OFF \
                -G "Unix Makefiles" $BUILD_GIT_ROOT/$PROJ/
            ;;

        stk)
            cmake \
                -DCMAKE_INSTALL_PREFIX=$PWD/install \
                -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
                -DCMAKE_DEBUG_POSTFIX="$BUILD_DEBUG_POSTFIX" \
                -DCMAKE_CXX_FLAGS="-fPIC" \
                -DBUILD_STATIC_LIBS=ON \
                -DBUILD_SHARED_LIBS=OFF \
                -G "Unix Makefiles" $BUILD_GIT_ROOT/$PROJ/
            ;;

        easySQLite | googletest)
            cmake \
                -DCMAKE_INSTALL_PREFIX=$PWD/install \
                -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
                -DCMAKE_DEBUG_POSTFIX="$BUILD_DEBUG_POSTFIX" \
                -DCMAKE_CXX_FLAGS="-fPIC" \
                -DCMAKE_C_FLAGS="-fPIC" \
                -DBUILD_STATIC_LIBS=ON \
                -DBUILD_SHARED_LIB=OFF \
                -G "Unix Makefiles" $BUILD_GIT_ROOT/$PROJ/
            ;;

        x265)
            cmake \
                -DCMAKE_INSTALL_PREFIX=$PWD/install \
                -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
                -DCMAKE_DEBUG_POSTFIX="$BUILD_DEBUG_POSTFIX" \
                -DCMAKE_C_FLAGS="-fPIC" \
                -DENABLE_SHARED=OFF \
                -G "Unix Makefiles" $BUILD_GIT_ROOT/$PROJ/source
            ;;

        libjpeg-turbo)
            cmake \
                -DCMAKE_INSTALL_PREFIX=$PWD/install \
                -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
                -DCMAKE_DEBUG_POSTFIX="$BUILD_DEBUG_POSTFIX" \
                -DCMAKE_C_FLAGS="-fPIC" \
                -DENABLE_SHARED=OFF \
                -G "Unix Makefiles" $BUILD_GIT_ROOT/$PROJ/
            ;;
    esac
}

function config()
{
    parse

    for PROJ in $BUILD_PROJS; do

        echo "*** start config $PROJ $BUILD_TYPE $BUILD_OS $BUILD_ARCH"

        cd $BUILD_GIT_ROOT
        cd $PROJ
        mkdir -p $BUILD_SUB_DIR;

        case $PROJ in
            openssl|osip|exosip|x264|ffmpeg|jpeg|net-snmp-5.8)

                do_automake $PROJ
                ;;

            jsoncpp|SDL|jthread|jrtplib|libevent|log4x|j2x|stk|easySQLite|x265|libjpeg-turbo|googletest)
                cd $BUILD_SUB_DIR

                if [ -f CMakeCache.txt ]; then
                    rm -fr CMakeCache.txt
                fi

                do_cmake $PROJ
                ;;
            curl)
                if [ $BUILD_OS = android ]; then
                    do_automake $PROJ
                else
                    cd $BUILD_SUB_DIR

                    if [ -f CMakeCache.txt ]; then
                        rm -fr CMakeCache.txt
                    fi

                    do_cmake $PROJ
                fi
                ;;
        esac

        make clean

        echo "*** end config $PROJ $BUILD_TYPE $BUILD_OS $BUILD_ARCH"
    done
}

function build()
{
    parse

    for PROJ in $BUILD_PROJS; do

        echo "*** start build $PROJ $BUILD_TYPE $BUILD_OS $BUILD_ARCH"

        cd $BUILD_GIT_ROOT/$PROJ

        case $PROJ in
            openssl|osip|exosip|x264|ffmpeg|jpeg|net-snmp-5.8)

                if [ ! -f Makefile ]; then
                    do_automake $PROJ
                fi
                ;;

            jsoncpp|SDL|jthread|jrtplib|libevent|log4x|j2x|stk|easySQLite|x265|libjpeg-turbo|googletest)

                if [ -d $BUILD_SUB_DIR ]; then
                    cd $BUILD_SUB_DIR

                    if [ ! -f Makefile ]; then
                        do_cmake $PROJ
                    fi
                else
                    mkdir -p $BUILD_SUB_DIR
                    cd $BUILD_SUB_DIR
                    do_cmake $PROJ
                fi
                ;;

            curl)
                if [ $BUILD_OS = android ]; then
                    if [ ! -f Makefile ]; then
                        do_automake $PROJ
                    fi
                else
                    if [ -d $BUILD_SUB_DIR ]; then
                        cd $BUILD_SUB_DIR

                        if [ ! -f Makefile ]; then
                            do_cmake $PROJ
                        fi
                    else
                        mkdir -p $BUILD_SUB_DIR
                        cd $BUILD_SUB_DIR
                        do_cmake $PROJ
                    fi
                fi
                ;;
        esac

        make $BUILD_MAKE_ARGS
        make install

        cd $BUILD_GIT_ROOT/$PROJ
        cp $BUILD_SUB_DIR_INC/* $BUILD_INSTALL_INC/ -fr

        # jsoncpp/libjpeg-turbo is special
        if [ $PROJ = jsoncpp -a $BUILD_ARCH = x64 ] || [ $PROJ = libjpeg-turbo -a $BUILD_ARCH = x64 ] || [ $PROJ = googletest -a $BUILD_ARCH = x64 ]; then
            cp ${BUILD_SUB_DIR_LIB}64/*.a $BUILD_INSTALL_LIB -fr
        else
            if [ $BUILD_TYPE = debug ]; then
                for FILE in $(ls $BUILD_SUB_DIR_LIB | grep "\.a" | grep -v "${BUILD_DEBUG_POSTFIX}\.a" )
                do
                    # BASE_NAME=$(echo $FILE | cut -d. -f1)
                    BASE_NAME=$(basename $FILE '.a')
                    cp $BUILD_SUB_DIR_LIB/$FILE -fr $BUILD_SUB_DIR_LIB/${BASE_NAME}$BUILD_DEBUG_POSTFIX.a
                done

                for FILE in $(ls $BUILD_SUB_DIR_LIB | grep "\.lib" | grep -v "${BUILD_DEBUG_POSTFIX}\.lib" )
                do
                    BASE_NAME=$(basename $FILE '.lib')
                    cp $BUILD_SUB_DIR_LIB/$FILE -fr $BUILD_SUB_DIR_LIB/${BASE_NAME}$BUILD_DEBUG_POSTFIX.lib
                done
            fi

            if [ $PROJ = ffmpeg -a $BUILD_OS = mingw-msvc ]; then
                for FILE in $(ls $BUILD_SUB_DIR_LIB | grep "\.a")
                do
                    BASE_NAME=$(basename $FILE '.a')
                    cp $BUILD_SUB_DIR_LIB/$FILE -fr $BUILD_SUB_DIR_LIB/${BASE_NAME}.lib
                done
            fi

            if [ -n "$(ls $BUILD_SUB_DIR_LIB |grep '\.a')" ]; then
                cp $BUILD_SUB_DIR_LIB/*.a $BUILD_INSTALL_LIB/ -fr
            fi

            if [ -n "$(ls $BUILD_SUB_DIR_LIB |grep '\.lib')" ]; then
                cp $BUILD_SUB_DIR_LIB/*.lib $BUILD_INSTALL_LIB/ -fr
            fi

        fi

        echo "*** end build $PROJ $BUILD_TYPE $BUILD_OS $BUILD_ARCH"
    done
}

function clean()
{
    parse

    for PROJ in $BUILD_PROJS; do

        echo "*** start clean $PROJ $BUILD_TYPE $BUILD_OS $BUILD_ARCH"

        cd $BUILD_GIT_ROOT/$PROJ
        if [ -f Makefile ]; then
            make clean
            # rm -fr Makefile
        fi

        # if [ -f configure ]; then
        #     rm -fr configure
        # fi

        if [ -d $BUILD_SUB_DIR ]; then
            cd $BUILD_SUB_DIR
            if [ ! -f Makefile ]; then
                make clean
                rm -fr Makefile
            fi
        fi

        cd $BUILD_GIT_ROOT/$PROJ

        rm -fr $BUILD_SUB_DIR_INSTALL

        # cd $BUILD_GIT_ROOT
        # rm -fr $BUILD_INSTALL

        echo "*** end clean $PROJ $BUILD_TYPE $BUILD_OS $BUILD_ARCH"
    done
}

function tag()
{
    echo "tag ...."
    # tar jcvf xxxxx
}

function pack()
{
    echo "pack ...."
    # tar jcvf xxxxx
}

function update()
{
    for PROJ in $BUILD_PROJS; do
        echo "*** start update $PROJ"
        cd $BUILD_GIT_ROOT/$PROJ
        git pull
        git status
        echo "*** end update $PROJ"
    done
}

function run()
{
    cd ..
    BUILD_GIT_ROOT=$PWD

    case $ACTION in
        rebuild)
            clean
            build
            ;;
        clean)
            clean
            ;;
        config)
            config
            ;;
        tag)
            tag
            ;;
        pack)
            pack
            ;;
        update)
            update
            ;;
        build|*)
            build
            ;;
    esac

    cd $BUILD_SCRIPT_DIR
}

run

echo "*** $ACTION $BUILD_TYPE $BUILD_OS $BUILD_ARCH OK!!!"
